// pages/we/diary/diary.js
import utils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    diarys: [],
    sources: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDiary(wx.getStorageSync('user')._openid, 0).then(res => {
      this.toDay(res.result.list, 0)
    })
  },
  toDay(diarys, type) {
    const db = wx.cloud.database()
    const _ = db.command
    let _openid = wx.getStorageSync('user')._openid
    db.collection('event').where(_.or([
      { _openid: _openid },
      { lover_openid: _openid }
    ])).get().then(res => {
      let start_num
      if(res.data.length == 0) start_num = Date.parse(new Date(utils.formatTime3(new Date)))
      else start_num = Date.parse(new Date(res.data[0].time))
      diarys.forEach(item => {
        let month = item.time.substr(5, 2)
        let day = item.time.substr(8, 2)
        var year = item.time.substr(0, 4)
        let end_num = Date.parse(new Date(year + '/' + month + '/' + day))
        var usedTime = end_num - start_num; //两个时间戳相差的毫秒数
        item.today = Math.floor(usedTime / (24 * 3600 * 1000))
      })
      if(type == 0){
        this.setData({
          diarys: diarys
        })
      }else if(type == 1){
        this.setData({
          diarys: this.data.diarys.concat(diarys)
        })
      }
    })
  },
  getDiary(_openid, skiplength) {
    return wx.cloud.callFunction({
      name: 'Diary',
      data: {
        _openid: _openid,
        skiplength: skiplength,
        type: 'select'
      }
    })
  },
  preview(e) {
    let index = e.currentTarget.dataset.index
    let i = e.currentTarget.dataset.i
    this.data.sources = []
    this.data.diarys[index].urls.forEach(item => { // 希望微信官方可以修改一下 这个接口 还要重新写一个source 真的太麻烦了
      this.data.sources.push({
        url: item,
        type: this.data.diarys[index].type
      })
    })
    wx.previewMedia({
      sources: this.data.sources,
      type: this.data.sources[i].type,
      url: this.data.sources[i].url,
      current: i,
      success: res => {
      },
      fail: res => {
      }
    })
  },
  deleteDiary(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗?',
      showCancel: true,
      success: res => {
        if (res.confirm) {
          var diary = this.data.diarys[e.currentTarget.dataset.index]
          this.data.diarys.splice(e.currentTarget.dataset.index, 1)
          wx.showLoading({
            title: '删除中',
          })
          wx.cloud.callFunction({
            name: 'Diary',
            data: {
              type: 'delete',
              diary: diary
            }
          }).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: '删除成功',
            })
            this.setData({
              diarys: this.data.diarys
            })
          })
        }
      }
    })
  },
  gotoedit(e) {
    let jsonStr = encodeURIComponent(JSON.stringify(this.data.diarys[e.currentTarget.dataset.index]))
    wx.navigateTo({
      url: '/pages/we/editdiary/editdiary?diary=' + jsonStr,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showLoading({
      title: '加载中',
    })
    this.getDiary(wx.getStorageSync('user')._openid, 0).then(res => {
      if (res.result.list.length == 0) {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        wx.showToast({
          icon: 'none',
          title: '没有数据啦',
        })
        return
      }
      this.toDay(res.result.list,0)
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
    })
    this.getDiary(wx.getStorageSync('user')._openid, this.data.diarys.length).then(res => {
      if (res.result.list.length <= 0) {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: '没有更多数据啦',
        })
        return
      }
      this.toDay(res.result.list,1)
      wx.hideLoading()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})